// const opt = require('./../../util/operate') //工具方法 mongoose
const Team = require('../../util/team') //教练 schema

let team = (router, opt) => {
	// 查询教练列表
	router.post('/getTeamList', async(ctx, next) => {
	    // 查询得到数据 并进行分割，返回部分数据 
	    var arr = [],
	      res = await opt.findAll(Team),
	      i = 0,
	      json = {},
	      len = res.length;
	    for (; i < len; i++) {
	      json.name = res[i].username
	      json.age = res[i].userage;
	      json.label = res[i].label
	      json.brief = res[i].brief
	      json.avatar = res[i].avatar
	      json.id = res[i].id
	      arr.push(json)
	    }
	    console.log('get teadm');
	    ctx.body = {
	        res: 'ok',
	        status: 200,
	        data: arr
	      }
	      // 释放内存
	    arr = null;
	    json = null;
	  })
	  // 获取教练详细
	router.post('/getTeamDetail', async(ctx, next) => {
	    var id = ctx.request.body.id;
	    // var res = await opt.findById(Team,ctx.request.body.id)
	    var detail = '获取信息失败';
	    if (id) {
	      detail = await opt.findById(Team, id)
	    }

	    ctx.body = {
	      res: 'ok',
	      status: 200,
	      data: detail
	    }

	  })
	  //新建教练信息
	router.post('/setTeamDetail', async(ctx, next) => {

	    var result = await opt.insert(Team, ctx.request.body)
	    ctx.body = {
	      res: result,
	      status: 200
	    }
	  })
	  //更新教练信息
	router.post('/updateTeamDetail', async(ctx, next) => {
	    var result = await opt.findByIdAndUpdate(Team, ctx.request.body.id, ctx.request.body.data)
	    ctx.body = {
	      res: '更新教练',
	      status: 200,
	      result: result
	    }
	  })
	}

module.exports = team